package com.leo.base.event;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;

/**
 * @ClassName EventBusCenter
 * @Description 事件中心
 * @Author wangss
 * @date 2020.09.20 19:54
 * @Version 1.0
 */
@Component
public class EventBusCenter {

    /**
     * 管理同步事件
     */
    private static final EventBus syncEventBus = new EventBus();

    /**
     * 管理异步事件
     */
    private static final AsyncEventBus asyncEventBus = new AsyncEventBus(Executors.newCachedThreadPool());

    /**
     * 注册同步事件
     *
     * @param event
     */
    public static void registerSync(Object event) {
        syncEventBus.register(event);
    }

    /**
     * 触发同步事件
     *
     * @param event
     */
    public static void postSync(Object event) {
        syncEventBus.post(event);
    }

    /**
     * 移除同步事件
     *
     * @param event
     */
    public static void unregisterSync(Object event) {
        syncEventBus.unregister(event);
    }

    /**
     * 注册异步事件
     *
     * @param event
     */
    public static void registerAsync(Object event) {
        asyncEventBus.register(event);
    }

    /**
     * 触发异步事件
     *
     * @param event
     */
    public static void postAsync(Object event) {
        asyncEventBus.post(event);
    }

    /**
     * 移除异步事件
     *
     * @param event
     */
    public static void unregisterAsync(Object event) {
        asyncEventBus.unregister(event);
    }
}
